package com.medical.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.domain.FormTemplate;
import com.medical.data.domain.TemplateForm;
import com.medical.data.exception.MyException;
import com.medical.data.mapper.TemplateFormMapper;
import com.medical.data.service.FormTemplateService;
import com.medical.data.service.TemplateFormService;
import com.medical.data.utils.ResultCode;
import com.medical.data.vo.AllFormVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-02-11
 */
@Service
@Slf4j
public class TemplateFormServiceImpl extends ServiceImpl<TemplateFormMapper, TemplateForm> implements TemplateFormService {

    @Autowired
    private FormTemplateService formTemplateService;

    @Override
    public List<String> getTemplateForm(AllFormVo allFormVo) {
        List<TemplateForm> templateForms = baseMapper.selectList(Wrappers.<TemplateForm>lambdaQuery()
                .eq(TemplateForm::getNodeId, allFormVo.getNodeId())
                .eq(TemplateForm::getProjectId, allFormVo.getProjectId()));
        List<String> ids = new ArrayList<>();
        if (!CollectionUtils.isEmpty(templateForms)) {
            ids = templateForms.stream().map(TemplateForm::getFormId).collect(Collectors.toList());
        }

        return ids;
    }

    @Override
    public Map<String, List<FormTemplate>> getForms(List<String> nodeIds) {
        Map<String, List<String>> map = new HashMap<>();
        Map<String, List<FormTemplate>> formTemplateMap = new HashMap<>();
        //首先查询该节点下的所有表单
        for (String nodeId : nodeIds) {
            List<TemplateForm> templateForms = baseMapper.selectList(Wrappers.<TemplateForm>lambdaQuery().eq(TemplateForm::getNodeId, nodeId).select(TemplateForm::getFormId));
            if (CollectionUtils.isEmpty(templateForms))
                map.put(nodeId, new ArrayList<>());
            List<String> formIds = templateForms.stream().map(TemplateForm::getFormId).collect(Collectors.toList());
            map.put(nodeId, formIds);
        }
        //查询表单名字
        System.out.println("========================>map" + map.entrySet());
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            List<FormTemplate> formTemplates = formTemplateService.getFormByIds(entry.getValue());
            if (CollectionUtils.isEmpty(formTemplates))
                formTemplateMap.put(entry.getKey(), new ArrayList<>());
            formTemplateMap.put(entry.getKey(), formTemplates);
        }
        return formTemplateMap;
    }

    @Override
    public void getFormByFormId(String formId, @NotBlank String nodeId) {
        LambdaQueryWrapper<TemplateForm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateForm::getFormId, formId)
                .eq(TemplateForm::getNodeId, nodeId);
        Long l = baseMapper.selectCount(wrapper);
        if (l < 1)
            throw new MyException(ResultCode.ERROR.getCode(), "该表单没有导入到节点中");
    }

    @Override
    public void deleteNodeById(String nodeId) {
        LambdaQueryWrapper<TemplateForm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateForm::getNodeId, nodeId);
        List<TemplateForm> templateForms = this.baseMapper.selectList(wrapper);
        if (templateForms.size() != 0) {
            int delete = this.baseMapper.delete(wrapper);
            if (delete < 1) {
                throw new MyException(ResultCode.ERROR.getCode(), "删除节点表单失败");
            }
        }

    }

    @Override
    public void deleteFormById(String formId) {
        LambdaQueryWrapper<TemplateForm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateForm::getFormId, formId);
        List<TemplateForm> templateForms = this.baseMapper.selectList(wrapper);
        if (templateForms.size() != 0) {
            int delete = this.baseMapper.delete(wrapper);
            if (delete < 1) {
                throw new MyException(ResultCode.ERROR.getCode(), "删除表单失败");
            }
        }

    }

    @Override
    public String getNodeIdByFormId(String formId) {
        LambdaQueryWrapper<TemplateForm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateForm::getFormId, formId);
        return this.baseMapper.selectOne(wrapper).getNodeId();
    }

    @Override
    public String getProjectIdByNodeId(String nodeId) {
        LambdaQueryWrapper<TemplateForm>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TemplateForm::getNodeId,nodeId);
        List<TemplateForm> templateForms = this.baseMapper.selectList(wrapper);
        return templateForms.get(0).getProjectId();
    }
}
